<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="js/vue.js"></script>
    <div id="app">
        <p>{{counter}}</p>
        <cpn :child-counter="counter" @change="changeTotal" ></cpn>
    </div>

    <!-- 1.创建模板 -->
    <template id="cpnT">
        <div>
            <button @click="increase">+</button>
            <button @click="decrease">-</button>
        </div>
    </template>

    <script>
        //创建子组件对象
        const cpn = {
            template:`#cpnT`,
            data(){
                return{
                    dCounter:this.childCounter
                }
            },
            props:{
                childCounter:Number
            },
            methods:{
                increase(){
                    this.dCounter++
                    this.$emit("change",this.dCounter)
                },
                decrease(){
                    this.dCounter--
                    this.$emit("change",this.dCounter)
                }
            }
        }
        var vm = new Vue({
            el: "#app",
            data: {
                counter:0
            },
            components:{
                cpn
            },
            methods:{
                changeTotal(value){
                    this.counter = value
                }
            }
        })
    </script>

</body>

</html>